package org.ss.psci.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.ss.psci.entity.PmAssignment;
import org.ss.psci.entity.PmContracts;
import org.ss.psci.entity.PmProjects;
import org.ss.psci.service.BaseDAO;
import org.ss.psci.service.ProjectService;
import org.ss.psci.service.TaskService;
import org.ss.psci.util.JsonUtil;

public class ShowDataController extends MultiActionController {

	private String mainView;
	private BaseDAO baseDAO;
	private TaskService taskService;
	private ProjectService projectService;

	public String getMainView() {
		return mainView;
	}

	public void setMainView(String mainView) {
		this.mainView = mainView;
	}

	public ModelAndView showProjects(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showProjects");
	}

	public ModelAndView showBid(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {

		return new ModelAndView("/showBid");
	}

	public ModelAndView contractsManage(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {

		return new ModelAndView("/contractsManage");
	}

	public ModelAndView taskManage(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {

		return new ModelAndView("/taskManage");
	}

	public ModelAndView showAssignment(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showAssignment");
	}

	public ModelAndView showContracts(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showContracts");
	}

	public ModelAndView showMessage(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showMessage");
	}

	public ModelAndView showProcess(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showProcess");
	}

	public ModelAndView showNote(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showNote");
	}

	public ModelAndView showEnd(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showEnd");
	}

	public ModelAndView showError(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showError");
	}

	public ModelAndView showCheck(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		return new ModelAndView("/showCheck");
	}

	public ModelAndView showConDep(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String projectsId = request.getSession().getAttribute("projectsId")
				.toString();
		String consId = request.getParameter("consId");
		if (consId == null) {
			consId = "ALL";
		}
		request.getSession().setAttribute("consId", consId);
		return new ModelAndView("/showConDep");
	}

	public ModelAndView showConProcess(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String projectsId = request.getSession().getAttribute("projectsId")
				.toString();
		String consId = request.getParameter("consId");
		if (consId == null) {
			consId = "ALL";
		}
		request.getSession().setAttribute("consId", consId);

		return new ModelAndView("/showConProcess");
	}

	public ModelAndView showChooseCons(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String username = request.getSession().getAttribute("username")
				.toString();
		String companyId = request.getSession().getAttribute("companyId")
				.toString();
		String projectsId = request.getSession().getAttribute("projectsId")
		.toString();
		
		List cons = projectService.getPmContracts(projectsId);
		List list = new ArrayList();

		for (int i = 0; i < cons.size(); i++) {
			PmContracts p = (PmContracts) cons.get(i);
			Map map = new HashMap();
			map.put("consId", p.getConsId());
			map.put("consName", p.getConsName());
			list.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
		response.setContentType("text/html");
		response.setCharacterEncoding("gb2312");
		response.getWriter().write(jsonArray.toString());

		return null;
	}
	public ModelAndView showAssignments(HttpServletRequest request,
			HttpServletResponse response, Object command) throws Exception {
		String username = request.getSession().getAttribute("username")
				.toString();
		String companyId = request.getSession().getAttribute("companyId")
				.toString();
		String projectsId = request.getSession().getAttribute("projectsId")
		.toString();
		String consId = request.getParameter("consId");
		List assignments = taskService.getPmAssignments(projectsId,consId);
		List list = new ArrayList();

		for (int i = 0; i < assignments.size(); i++) {
			PmAssignment p = (PmAssignment) assignments.get(i);
			Map map = new HashMap();
			map.put("assignmentId", p.getAssignmentId());
			map.put("name", p.getName());
			list.add(map);
		}
		JSONArray jsonArray = JSONArray.fromObject(list);
		System.out.println(jsonArray.toString());
		response.setContentType("text/html");
		response.setCharacterEncoding("gb2312");
		response.getWriter().write(jsonArray.toString());

		return null;
	}
	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

}
